package net.wsfcp.android.ui.view.my.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.wsfcp.android.R;
import net.wsfcp.android.adapter.FragmentAdapter;
import net.wsfcp.android.base.BaseActivity;
import net.wsfcp.android.bean.resultbean.OrderQuantityBean;
import net.wsfcp.android.http.mvp.contract.OrderListContract;
import net.wsfcp.android.event.ReloadOrderEvent;
import net.wsfcp.android.http.mvp.presenter.OrderActivityPresenter;
import net.wsfcp.android.ui.view.my.fragment.OrderFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

//订单列表总页面
public class OrderListActivity extends BaseActivity<OrderActivityPresenter> implements OrderListContract.OrderActivityView {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_right)
    Button btnRight;

    @BindView(R.id.ll_todo)
    LinearLayout llTodo;
    @BindView(R.id.img_todo)
    ImageView imgTodo;
    @BindView(R.id.txt_todo)
    TextView txtTodo;
    @BindView(R.id.ll_send)
    LinearLayout llSend;
    @BindView(R.id.img_send)
    ImageView imgSend;
    @BindView(R.id.txt_send)
    TextView txtSend;
    @BindView(R.id.ll_finish)
    LinearLayout llFinish;
    @BindView(R.id.img_finish)
    ImageView imgFinish;
    @BindView(R.id.txt_finish)
    TextView txtFinish;
    @BindView(R.id.ll_delete)
    LinearLayout llDelete;
    @BindView(R.id.img_delete)
    ImageView imgDelete;
    @BindView(R.id.txt_delete)
    TextView txtDelete;
    @BindView(R.id.vp)
    ViewPager vp;

    int checkType = 0;//0 未处理  1配送中   2已完成   3已删除
    ArrayList<LinearLayout> linearLayouts = new ArrayList<>();
    ArrayList<ImageView> imageViews = new ArrayList<>();
    ArrayList<TextView> textViews = new ArrayList<>();
    ArrayList<Integer> checkImgs = new ArrayList<>();
    ArrayList<Integer> uncheckImgs = new ArrayList<>();
    ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_orderlist;
    }

    @Override
    protected OrderActivityPresenter loadPresenter() {
        return new OrderActivityPresenter();
    }

    @Override
    protected void initData() {
        setmTitle(getString(R.string.fragment_my_order));
        setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mPresenter.getOrderQuantity();
    }

    @Override
    protected void initListener() {
        llTodo.setOnClickListener(this);
        llSend.setOnClickListener(this);
        llFinish.setOnClickListener(this);
        llDelete.setOnClickListener(this);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                checkLayout(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void otherViewClick(View view) {
        switch (view.getId()) {
            case R.id.ll_todo:
                checkType = 0;
                break;
            case R.id.ll_send:
                checkType = 1;
                break;
            case R.id.ll_finish:
                checkType = 2;
                break;
            case R.id.ll_delete:
                checkType = 3;
                break;
        }
        vp.setCurrentItem(checkType);
        checkLayout(checkType);

    }

    @Override
    protected void initView() {
        btnLeft = rlLeft;
        mTitle = txtTitle;
        btRight = btnRight;

        initTab();
        initFragment();

    }

    public void initTab() {
        linearLayouts.clear();
        imageViews.clear();
        textViews.clear();
        linearLayouts.add(llTodo);
        linearLayouts.add(llSend);
        linearLayouts.add(llFinish);
        linearLayouts.add(llDelete);
        imageViews.add(imgTodo);
        imageViews.add(imgSend);
        imageViews.add(imgFinish);
        imageViews.add(imgDelete);
        textViews.add(txtTodo);
        textViews.add(txtSend);
        textViews.add(txtFinish);
        textViews.add(txtDelete);
        checkImgs.add(R.mipmap.order_icon_02);
        checkImgs.add(R.mipmap.order_icon_04);
        checkImgs.add(R.mipmap.order_icon_06);
        checkImgs.add(R.mipmap.order_icon_08);
        uncheckImgs.add(R.mipmap.order_icon_03);
        uncheckImgs.add(R.mipmap.order_icon_05);
        uncheckImgs.add(R.mipmap.order_icon_07);
        uncheckImgs.add(R.mipmap.order_icon_09);
    }

    public void initFragment() {
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());

        for (int i = 0; i < linearLayouts.size(); i++) {
            OrderFragment orderFragment = new OrderFragment();
            Bundle bundle = new Bundle();
            if (i == 3) {
                bundle.putInt("type", -1);
            } else {
                bundle.putInt("type", i);
            }
            orderFragment.setArguments(bundle);

            fragments.add(orderFragment);
        }
        fragmentAdapter.setmFragment(fragments);

        vp.setAdapter(fragmentAdapter);
        vp.setOffscreenPageLimit(3);//设置预加载3个
    }


    private void checkLayout(int checkType) {
        for (int i = 0; i < linearLayouts.size(); i++) {
            if (i == checkType) {
                linearLayouts.get(i).setBackgroundResource(R.drawable.bill_check);
                imageViews.get(i).setImageResource(checkImgs.get(i));
                textViews.get(i).setTextColor(getResources().getColor(R.color.white));
            } else {
                linearLayouts.get(i).setBackgroundResource(R.drawable.bill_uncheck);
                imageViews.get(i).setImageResource(uncheckImgs.get(i));
                textViews.get(i).setTextColor(getResources().getColor(R.color.theme_color));
            }
        }

    }


    @Override
    public void getOrderQuantitySuccess(OrderQuantityBean bean) {
        textViews.get(0).setText("未处理(" + bean.getUntreatedQuantity() + ")");
        textViews.get(1).setText("配送中(" + bean.getDispatchingQuantity() + ")");
        textViews.get(2).setText("已完成(" + bean.getCompletedQuantity() + ")");
        textViews.get(3).setText("已删除(" + bean.getInvalidQuantity() + ")");

    }

    @Override
    public void getOrderQuantityFail(String failMsg) {
        toast(failMsg);
        textViews.get(0).setText("未处理(" + 0 + ")");
        textViews.get(1).setText("配送中(" + 0 + ")");
        textViews.get(2).setText("已完成(" + 0 + ")");
        textViews.get(3).setText("已删除(" + 0 + ")");
    }

    //接收订阅的事件
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void reloadList(ReloadOrderEvent event) {
        mPresenter.getOrderQuantity();

    }
}
